package com.example.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 二七和夭夭
 * @email 2646498606@qq.com
 * @date 2022/8/24 15:41
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("loginUser");

        //判断是否有登录用户
        if (user == null) {
            response.sendRedirect(request.getContextPath() + "/jsp/login.jsp");
            return false;
        }
        return true;
    }
}
